export const findNodeUpperByClasses = (ele, classes) => {
  let parentNode = ele.parentNode;
  if (parentNode) {
    let classList = parentNode.classList;
    if (
      classList &&
      classes.every(className => classList.contains(className))
    ) {
      return parentNode;
    } else {
      return findNodeUpperByClasses(parentNode, classes);
    }
  }
};

// deepCopy
export const deepCopy = data => {
  const t = typeof data;
  let o;
  if (t === "array") {
    o = [];
  } else if (t === "object") {
    o = {};
  } else {
    return data;
  }

  if (t === "array") {
    for (let i = 0; i < data.length; i++) {
      o.push(deepCopy(data[i]));
    }
  } else if (t === "object") {
    for (let i in data) {
      o[i] = deepCopy(data[i]);
    }
  }
  return o;
};

// 数字****格式化
export const drawFormat = (str, type, start, end) => {
  if (str == "" || str == null) return str;
  let val = typeof str === "number" ? str.toString() : str;
  let len = val.length;
  return (
    val.substring(0, start) +
    Array(len - start - end + 1).join(type) +
    val.substring(len - end)
  );
};

//  99999 => 99,999
export const formatTreeMount = num => {
  if (num) {
    if (typeof num === "number") {
      num += "";
    }
    var pattern = /(?=((?!\b)\d{3})+$)/g;
    return num.replace(pattern, ",");
  }else if (num==0) {
    return 0;
  }
};


// get request Object to string
export const formatRequestStr = params => {
  let query = '?';
  Object.keys(params).forEach((item, index) => {
    if (params[item] || params[item] === 0) {
      query += `${index === 0 ? "" : "&"}${item}=${params[item]}`;
    }
  });
  if (query === '?') {
    return '';
  }
  return query;
}
